package com.wuyi.tea.controller.admin;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuyi.tea.domain.dto.ProductDTO;
import com.wuyi.tea.domain.vo.ProductVO;
import com.wuyi.tea.domain.vo.R;
import com.wuyi.tea.service.ProductService;
import com.wuyi.tea.util.LocalStorageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Api(tags = "商品模块")
@RestController(value = "AdminProductController")
@RequestMapping("/admin/product")
public class ProductController {

    @Resource
    private ProductService productService;

    @ApiOperation("添加商品")
    @PostMapping
    public R addProduct(@RequestBody ProductDTO productDTO){

        log.info("id为{}的用户正在添加商品：\n{}", LocalStorageUtil.get(),productDTO);
        productService.addProduct(productDTO);
        return R.success("添加成功",null);
    }

    @ApiOperation("修改商品信息")
    @PutMapping
    public R updateProduct(@RequestBody ProductDTO productDTO){

        log.info("id为{}的用户正在修改商品信息：\n{}", LocalStorageUtil.get(),productDTO);
        productService.updateProduct(productDTO);
        return R.success("修改成功",null);
    }

    @ApiOperation("获取商品列表")
    @GetMapping("/list")
    public R list(
                @RequestParam(name = "key",required = false) String key,
                @RequestParam(name = "saleStatus",defaultValue = "1",required = false) Integer saleStatus,
                @RequestParam(name = "categoryId",required = false) Long categoryId,
                @RequestParam(name = "priceMin",defaultValue = "0",required = false) Integer priceMin,
                @RequestParam(name = "priceMax",required = false) Integer priceMax,
                @RequestParam(name = "page",defaultValue = "1",required = false) Integer page,
                @RequestParam(name = "pageSize",defaultValue = "5",required = false) Integer pageSize
                    ) {

        log.info("id为{}的用户正在获取商品列表,\n筛选条件：key: {} ,saleStatus: {} , categoryId: {} ,priceMin: {} , " +
                "priceMax: {} , page: {} , pageSize: {}",key,saleStatus,categoryId,priceMin,priceMax,page,pageSize);
        Page<ProductVO> productVOPage = productService.list(key,saleStatus,categoryId,priceMin,priceMax,page,pageSize);
        return R.success("获取成功",productVOPage);
    }

    @ApiOperation("删除商品")
    @DeleteMapping
    public R deleteProduct(@RequestParam("ids") List<Integer>ids){

        log.info("id为{}的用户正在删除商品：\n{}",LocalStorageUtil.get(),ids);
        productService.deleteProduct(ids);
        return R.success("删除成功",null);
    }

}
